﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections.Generic;
using WXYW.Model;

namespace WXYW
{
    public partial class ManagerCustomer : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCustomerList();
            }
        }

        private void BindCustomerList()
        {
            UserService userService = new UserService();
            List<SimpleUserModel> users = userService.GetAllUserList(keyWordTextBox.Text);
            RecordsNumber = users.Count;
            customerGridView.DataSource = users;
            customerGridView.DataBind();
        }

        protected void customerGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= customerGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (customerGridView.PageIndex + 1).ToString();
            }
        }

        protected void customerGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            UserService userService = new UserService();
            if (e.CommandName.Equals("DeleteUser"))
            {
                try
                {
                    userService.DeleteUser(Convert.ToInt64(e.CommandArgument));
                    MsgLbl.Text = "用户已经成功删除。";
                    BindCustomerList();
                }
                catch (Exception ex)
                {
                    MsgLbl.Text = string.Format("系统错误：{0}", ex.Message);
                }
            }
            else if (e.CommandName.Equals("EditUser"))
            {
                Response.Redirect(string.Format("EditUser.aspx?{0}={1}", Constants.QUERYSTR_USER_ID, e.CommandArgument));
            }
        }

        protected void customerGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < customerGridView.PageCount)
            {
                customerGridView.PageIndex = e.NewPageIndex;
            }
            
            BindCustomerList();
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<SimpleUserModel> dds = new List<SimpleUserModel>();
            dds.Add(new SimpleUserModel());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            customerGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindCustomerList();
        }

        protected void sparePartApplyFormsGridView_DataBound(object sender, EventArgs e)
        {
            if (customerGridView.BottomPagerRow != null)
            {
                customerGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindCustomerList();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindCustomerList();
        }
    }
}
